package com.ymatou.iapi.filemanager.testcase;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.util.Arrays;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.filemanager.service.*;
import com.ymatou.img.api.service.ConfirmCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.FileMd5Wapper;
import com.ymttest.utils.FontImage;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 上传图片 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Upload {
	private static UploadCall uploadcall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("上传图片");
	}

	@Before
	public void caseUp() {
		uploadcall = new UploadCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(uploadcall.getOpurl());
		TestDataManager.removeTempFile();
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_001() {
		Logger.start(true, "上传csv成功");
		try {
			String turl = "";
			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "ptest01.zip";
			Logger.comment("添加上传文件:" + fliename);
			uploadcall = new UploadCall();
			uploadcall.addPart("file", fliename);

			uploadcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall.getMsg(),
					"验证Msg");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_002() {
		Logger.start(true, "上传exe文件成功");
		try {
			String turl = "";
			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.exe";
			Logger.comment("添加上传文件:" + fliename);
			uploadcall.addPart("file", fliename);

			uploadcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall.getMsg(),
					"验证Msg");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_003() {
		Logger.start(true, "上传测试图片成功");
		try {
			String turl = "";
			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "ERROR.err";
			Logger.comment("添加上传文件:" + fliename);
			String time=YMTDateUtil.getBeforeOrNextSecond(10);
			String url1=FormatUtil.urlEncode("expiredTime="+time);
			uploadcall = new UploadCall("?"+url1);
			uploadcall.addPart("file", fliename);

			uploadcall.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall.getMsg(),
					"验证Msg");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall.getFileUrl(), "验证文件地址非空");
			
			Thread.sleep(10000);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_004() {
		Logger.start(true, "上传图片成功,定时删除成功");
		try {
			String fliename="./temp/a.png";
			FontImage.createImage(fliename,Arrays.asList(TestDataManager.getRandomNum(10)), new Font("宋体", Font.BOLD, 30) , FontImage.getColorFmtAlpha(Color.ORANGE,0.8d), Color.WHITE);
			String type = "idcard";
			String turl = "";
			Logger.comment("setPicType:" + type);
			UploadImageCall uploadcall2=new UploadImageCall("?PicType="+type+"&expiredTime="+FormatUtil.urlEncode(YMTDateUtil.getBeforeOrNextSecond(5)));


			Logger.comment("添加上传文件:" + fliename);
			uploadcall2.addPart("file", fliename);

			uploadcall2.callService();

			Logger.verifyEquals(ECode.SUCCESS, uploadcall2.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall2.getMsg(),
					"验证Msg");
			String ou=uploadcall2.getPicUrl();
			Map m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("false", m1.get("Confirm").toString(), "验证Confirm");
			
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			Thread.sleep(5000);
			new RemoveNotConfirmFileJobCall().callService();
			Thread.sleep(5000);
			Integer csize=new FileMd5Wapper().selectConfirmFile(ou).size();
			Logger.verifyEquals(0, csize, "验证被移除");
			
			Integer exsize=new FileMd5Wapper().selectExpiredFile(ou).size();
			Logger.verifyEquals(1, exsize, "验证被移除");
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(false, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(false, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_005() {
		Logger.start(true, "上传图片成功,确认后不删除");
		try {
			String fliename="./temp/a.png";
			FontImage.createImage(fliename,Arrays.asList(TestDataManager.getRandomNum(10)), new Font("宋体", Font.BOLD, 30) , FontImage.getColorFmtAlpha(Color.ORANGE,0.8d), Color.WHITE);
			String type = "idcard";
			String turl = "";
			Logger.comment("setPicType:" + type);
			UploadImageCall uploadcall2=new UploadImageCall("?PicType="+type+"&expiredTime="+FormatUtil.urlEncode(YMTDateUtil.getBeforeOrNextSecond(5)));


			Logger.comment("添加上传文件:" + fliename);
			uploadcall2.addPart("file", fliename);

			uploadcall2.callService();
			String oturl = uploadcall2.getPicUrl();
			Logger.verifyEquals(ECode.SUCCESS, uploadcall2.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall2.getMsg(),
					"验证Msg");
			
			String ou=uploadcall2.getPicUrl();
			Map m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("false", m1.get("Confirm").toString(), "验证Confirm");
			
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			new ConfirmCall("?filePath="+oturl).callService();
			Thread.sleep(5000);
			m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("true", m1.get("Confirm").toString(), "验证Confirm");
			
			new RemoveNotConfirmFileJobCall().callService();
			
			m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("true", m1.get("Confirm").toString(), "验证Confirm");
			
			Integer exsize=new FileMd5Wapper().selectExpiredFile(ou).size();
			Logger.verifyEquals(0, exsize, "验证不被移除");
			
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Upload_006() {
		Logger.start(true, "上传图片成功,再次上传不带expiredTime-相当于确认后不删除");
		try {
			String fliename="./temp/a.png";
			FontImage.createImage(fliename,Arrays.asList(TestDataManager.getRandomNum(10)), new Font("宋体", Font.BOLD, 30) , FontImage.getColorFmtAlpha(Color.ORANGE,0.8d), Color.WHITE);
			String type = "idcard";
			String turl = "";
			Logger.comment("setPicType:" + type);
			UploadImageCall uploadcall2=new UploadImageCall("?PicType="+type+"&expiredTime="+FormatUtil.urlEncode(YMTDateUtil.getBeforeOrNextSecond(5)));


			Logger.comment("添加上传文件:" + fliename);
			uploadcall2.addPart("file", fliename);

			uploadcall2.callService();
			String oturl = uploadcall2.getPicUrl();
			Logger.verifyEquals(ECode.SUCCESS, uploadcall2.getStatus(),
					"验证Status");
			Logger.verifyEquals(ECode.PIC_UPLOAD_SUCCESS, uploadcall2.getMsg(),
					"验证Msg");
			
			String ou=uploadcall2.getPicUrl();
			Map m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("false", m1.get("Confirm").toString(), "验证Confirm");
			
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
//			turl = changeUrl(uploadcall.getFileUrl(), "");
			Logger.verifyNotNull(uploadcall2.getPicUrl(), "验证文件地址非空");
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			//new ConfirmCall("?filePath="+oturl).callService();
			uploadcall2=new UploadImageCall("?PicType="+type);


			Logger.comment("添加上传文件:" + fliename);
			uploadcall2.addPart("file", fliename);

			uploadcall2.callService();
			oturl = uploadcall2.getPicUrl();
			
			Thread.sleep(5000);
			m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("true", m1.get("Confirm").toString(), "验证Confirm");
			
			new RemoveNotConfirmFileJobCall().callService();
			
			m1=new FileMd5Wapper().selectConfirmFile(ou).get(0);
			Logger.verifyEquals("true", m1.get("Confirm").toString(), "验证Confirm");
			Integer exsize=new FileMd5Wapper().selectExpiredFile(ou).size();
			Logger.verifyEquals(0, exsize, "验证不被移除");
			turl = changeUrl(uploadcall2.getPicUrl(), "_o");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
			turl = changeUrl(uploadcall2.getPicUrl(), "_s");
			Logger.verifyEquals(true, TestDataManager.isPic(new File(TestDataManager.DownloadFile(turl))), "验证" + turl
					+ "是否是图片");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 修改url地址，本case专用 用于在原来的地址文件名后加后缀
	 * 
	 * @return 修改后的地址
	 */
	private static String changeUrl(String url, String suffix) {
		//http://file.iapi.ymatou.com/G01/M00/1B/EB/rBBlD19fMY2AUb0rAAAB9Ul1-iY133.png
		return url.substring(0, url.lastIndexOf("_")) + suffix
				+ url.substring(url.lastIndexOf("."), url.length());
	}
}